package com.techwells.learningkites.controller;

import java.util.HashMap;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.techwells.learningkites.R;
import com.techwells.learningkites.model.ForgetPasswordViewModel;
import com.techwells.learningkites.networkservice.Constant;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.utils.ToastUtils;

public class ForgetPasswordActivity extends CommonBaseActivity implements OnClickListener {
	
	private EditText username,verify,password;
	private Button getVerify,btnConfirm;
	
	private ForgetPasswordViewModel presentModel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget_password);
		
		initTitleBar(R.string.title_forgetpassword, defaultLeftClickListener);
		initViews();
	}

	private void initViews() {
		username = (EditText) findViewById(R.id.mobile);
		verify = (EditText) findViewById(R.id.verify);
		password = (EditText) findViewById(R.id.password);
		getVerify = (Button) findViewById(R.id.get_verify);
		btnConfirm = (Button) findViewById(R.id.btn_confirm);
		getVerify.setOnClickListener(this);
		btnConfirm.setOnClickListener(this);
		
		
		
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.get_verify:
			doRequestLostPassword();
			break;

		default:
			doRequestResetPassword();
			break;
		}
		
	}
	

	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		
		presentModel = (ForgetPasswordViewModel) baseViewModel;
	}
	

	private void doRequestLostPassword() {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("loginId", username.getText().toString());
		doTask(KitesServiceMediator.SERVICE_DO_PASSWORD_LOST,argMap);
		
	}
	
	
	private void doRequestResetPassword() {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("code", verify.getText().toString());
		argMap.put("password", password.getText().toString());
		doTask(KitesServiceMediator.SERVICE_DO_PASSWORD_RESET,argMap);
		
	}
	

	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
	}

	@Override
	public void requestFailedHandle(TaskToken token, int errorCode,
			String errorMsg) {
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_DO_PASSWORD_LOST)) {
			if (errorCode == 202) {
				ToastUtils.show(this, "验证码获取成功，请输入验证码");
			}else if (errorCode == 464) {
				ToastUtils.show(this, "用户不存在");
			}
		}if (method.equals(KitesServiceMediator.SERVICE_DO_PASSWORD_RESET)) {
			if (errorCode == 205) {
				ToastUtils.show(this, "密码修改成功");
				finish();
			}else if (errorCode == 461) {
				ToastUtils.show(this, "验证码无效");
			}
		}
		
	}
	
	
	
}
